//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: 1,
    mword: '',
    mean: '',
    Amean: '',
    Bmean: '',
    Cmean: '',
    Dmean: '',
    mpos: '',
    input: '',
    caculate: 1,
    errorCount: 0,
    flag: 0,
    rands: [],
    num: 20
  },

  //对选择项进行判断
  judge: function(e){
    if(e.currentTarget.dataset.text == "A"){
      if(this.data.Amean == app.globalData.means){
        this.next()
      }else{
        //标记重复错误，只计算一次错误
        if(this.data.flag === 0) {
          const errorCount = ++this.data.errorCount
          this.setData({
            errorCount,
            flag: 1
          })
        }
        this.next()
      }
    }
    if(e.currentTarget.dataset.text == "B"){
      if(this.data.Bmean == app.globalData.means){
        this.next()
      }else{
        //标记重复错误，只计算一次错误
        if(this.data.flag === 0) {
          const errorCount = ++this.data.errorCount
          this.setData({
            errorCount,
            flag: 1
          })
        }
        this.next()
      }
    }
    if(e.currentTarget.dataset.text == "C"){
      if(this.data.Cmean == app.globalData.means){
        this.next()
      }else{
        //标记重复错误，只计算一次错误
        if(this.data.flag === 0) {
          const errorCount = ++this.data.errorCount
          this.setData({
            errorCount,
            flag: 1
          })
        }
        this.next()
      }
    }
    if(e.currentTarget.dataset.text == "D"){
      if(this.data.Dmean == app.globalData.means){
        this.next()
      }else{
        //标记重复错误，只计算一次错误
        if(this.data.flag === 0) {
          const errorCount = ++this.data.errorCount
          this.setData({
            errorCount,
            flag: 1
          })
        }
        this.next()
      }
    }
  },

  // 下一个
  next: function(e) {
    // 答完选定题数时，跳转结束页面
    if(this.data.order == this.data.num){
      const errorCount = this.data.errorCount
      wx.setStorage({
        data: errorCount,
        key: 'err',
      })
      wx.setStorageSync('ci_ku', 'university')
      wx.setStorageSync('num', this.data.num)
      wx.redirectTo({
        url: '../finish/finish',
        success: function(res){},
        fail: function() {},
        complete: function() {}
      })
      return;
    }

    const order = ++this.data.order
    //当前进度
    const caculate = Math.floor((this.data.order/this.data.num)*100)
    this.setData({
      input: '',
      order,
      caculate,
      //重复错误标记复位
      flag: 0
    })

    //刷新一次随机数组和取出的数据，相当于刷新一次页面
    this.makelist()
    this.getData()
  },

  // 随机从数据库中抽取一条数据
  getData(){
    const db = wx.cloud.database()
      db.collection('university')
        .aggregate()  //aggreate下使用sample
        .sample({
          size: 4,  //随机数量为1个
        })
        .end()  //获取完成
        .then(res => {
          let Amean = res.list[this.data.rands[0]].content.word.content.trans[0].tranCn
          let Bmean = res.list[this.data.rands[1]].content.word.content.trans[0].tranCn
          let Cmean = res.list[this.data.rands[2]].content.word.content.trans[0].tranCn
          let Dmean = res.list[this.data.rands[3]].content.word.content.trans[0].tranCn
          if(Amean.length > 24){
            Amean = Amean.substring(0,24) + "..."
          }
          if(Bmean.length > 24){
            Bmean = Bmean.substring(0,24) + "..."
          }
          if(Cmean.length > 24){
            Cmean = Cmean.substring(0,24) + "..."
          }
          if(Dmean.length > 24){
            Dmean = Dmean.substring(0,24) + "..."
          }
          this.setData({
            mword: res.list[0].headWord,
            mean: res.list[0].content.word.content.trans[0].tranCn,
            Amean: Amean,
            Bmean: Bmean,
            Cmean: Cmean,
            Dmean: Dmean,
            })
            let words = res.list[0].headWord.trim()//value是具体数据
            let means = res.list[0].content.word.content.trans[0].tranCn.trim()
            app.globalData.words = words
            app.globalData.means = means
            //输出取出的对应单词
            // console.log("当前单词："+app.globalData.words)
            // console.log("当前意思："+app.globalData.means)
        })
  },

  //产生一个范围在【0-3】且每个都出现且不重的数组
  makelist(){
    let rand = []
    rand[0] = (Math.random() * 3).toFixed(0)

    rand[1] = (Math.random() * 3).toFixed(0)
    do{
      rand[1] = (Math.random() * 3).toFixed(0)
    }while(rand[1] == rand[0])

    rand[2] = (Math.random() * 3).toFixed(0)
    do{
      rand[2] =(Math.random() * 3).toFixed(0)
    }while(rand[2] == rand[1] || rand[2] == rand[0])

    rand[3] = (Math.random() * 3).toFixed(0)
    do{
      rand[3] =(Math.random() * 3).toFixed(0)
    }while((rand[3] == rand[2] || rand[3] == rand[1] || rand[3] == rand[0]))

    this.setData({
      rands: rand
    })
  },

  setcaculate(){
    var caculate1 = 0
    if(this.data.num === 10){
      caculate1 = 10
    } else if(this.data.num === 20){
      caculate1 = 5
    }else if(this.data.num === 30){
      caculate1 = 3
    }
    this.setData({
      caculate: caculate1
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.makelist()
    this.getData()
    wx.getStorage({
      key: 'num',
    }).then(
      res=>{
        const num = res.data
        this.setData({
          num
        })
      }
    ).then(
      ()=>{
        this.setcaculate()
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})